<?php

/**
 * Description of DataObject
 *
 * @author mabi
 */
abstract class DataObject {

    static $tableName;
    static $className = __CLASS__;
    static $fields = array();
    static $classTablePK;
    /**
     * pola wystepujace tylko w obiekatch (nie sa zapisywane do BD)
     *
     * @var array
     */
    static $classFields = array();
    protected $id;
    /** zdjecia * */

    /**
     * Export danych do tablicy asocjacyjnej z pol klasy.
     *
     * @return array()
     */
    public function ToArrayAssoc() {
	$fields = $this->GetFields();

	$done = array();

	foreach ($fields as $index => $value) {
	    $temp = 'Get' . $fields[$index];
	    $done[$index] = $this->$temp();
	}
	return $done;
    }

    /**
     * Import danych z tablicy asocjacyjnej do p�l klasy.
     *
     * @param string $array tablica asocjacyjna zawieraj�ca rekordy pobrane z bazy
     * @param int $cut 1 - klucze musz� mie� posta� <KLASA>_<POLE>, 0 - <POLE>
     */
    public function FromArray($array, $cut = false, $alias = null) {
	$fields = $this->GetFields();

	// sprawdzenie czy istnieje metoda dla pol niebedacych w BD
	if (method_exists($this, 'GetClassFields')) {
	    $fields = array_merge($fields, $this->GetClassFields());
	}

	$prfx = ((!empty($alias) && is_string($alias)) ? $alias . '_' : '' );

	if (is_array($array))
	    foreach ($array as $index => $var) {
		if ($cut == true)
		    $index = str_replace($prfx . $this->GetClassName() . "_", "", $index);
		if (array_key_exists($index, $fields)) {
		    $temp = 'Set' . $fields[$index];
		    $this->$temp($var);
		}
	    }
    }

    /**
     * Import danych z tablicy asocjacyjnej do p�l klasy.
     * Jedyn� r�nic� w por�wnaniu do @see FromArray jest zmiana postaci
     * klucza, kt�re teraz maja posta�: $prefix_<KLASA>_<POLE>
     *
     * @param string $array tablica asocjacyjna zawieraj�ca rekordy pobrane z bazy
     */
    public function FromArrayPrefix($array, $prefix) {
	$fields = $this->GetFields();
	if (is_array($array)) {
	    foreach ($array as $index => $var) {
		$index = str_replace($prefix . "_" . $this->GetClassName() . "_", "", $index);

		if (array_key_exists($index, $fields)) {
		    $temp = 'Set' . $fields[$index];
		    $this->$temp($var);
		}
	    }
	}
    }

    public function GetType() {
	return array_search($this->GetClassName(), Type::$types);
    }

    public abstract function GetId();

    public abstract function GetTableName();

    public abstract function GetFields();

    public abstract function GetClassName();

    public abstract function GetClassTablePK();

    public static function GetStaticVariable($variableName) {
	if (isset(self::$$variableName)) {
	    return self::$$variableName;
	} else {
	    throw new Exception('Klasa ' . __CLASS__ . ' nie posiada stałej: ' . $variableName . ' !');
	}
    }

    /**
     * Metoda zwraca tablice id'k�w tablicy obiekt�w.
     * Wykorzystywana na potrzeby DefaultDAL::ToJavaScriptArray($array, $jsName)
     *
     * @param array $objArray
     */
    public static function GetObjIDs($objArray) {
	$array = array();
	if (is_array($objArray)) {
	    foreach ($objArray as $obj) {
		$array[] = $obj->GetId();
	    }
	} else {
	    return $array;
	}
	return $array;
    }

    /**
     * Metoda zwraca tablice nazw
     * Wykorzystywana tylko w slownikach!
     *
     * @param unknown_type $objArray
     */
    public static function ToArray($objArray) {

	$done = array();
	foreach ($objArray as $value) {
	    $done[$value->GetId()] = $value->GetName();
	}
	return $done;
    }

    public static function ToArrayUrl($objArray) {

	$done = array();
	foreach ($objArray as $value) {
	    $done[$value->GetName()] = $value->GetUrlName();
	}
	return $done;
    }

    public function GetTitleAlt() {
	return strip_tags(Utils::cleanUrlSpaces($this->GetTitle()));
    }

    public function GetValueAlt() {
	return strip_tags(Utils::cleanUrlSpaces($this->GetValue()));
    }

    public function GetHeaderAlt() {
	return strip_tags(Utils::cleanUrlSpaces($this->GetHeader()));
    }

    public function GetNameAlt() {
	return strip_tags(Utils::cleanUrlSpaces($this->GetName()));
    }

    public function GetTitleUrl() {
	return Utils::TextToUrl(strip_tags(Utils::cleanUrl($this->GetTitle())));
    }

    public function GetValueUrl() {
	return Utils::TextToUrl(strip_tags(Utils::cleanUrl($this->GetValue())));
    }

    public function GetHeaderUrl() {
	return Utils::TextToUrl(strip_tags(Utils::cleanUrl($this->GetHeader())));
    }

    public function GetNameUrl() {
	return Utils::TextToUrl(strip_tags(Utils::cleanUrl($this->GetName())));
    }

    public function getFormatedDate($func = "date", $format = 1) {
	$func = "get" . $func;
	$date = $this->$func();

	switch ($format) {
	    default:
		$date = explode(" ", $date);
		return $date[0];
		break;
	}
    }

//	abstract public function GetUrl();

    public function GetDalDataObj($dalArray = array()) {
	$dalName = $this->GetClassName() . 'DAL';
	$dalData = null;
	eval('$dalData = ' . $dalName . '::GetDalDataObj($dalArray);');
	if (is_a($dalData, 'DalData'))
	    $dalData->setObj($this);
	return $dalData;
    }

}
